<?php
namespace app\index\controller;
use think\Db;
class Index extends Common{
  public function index()
  {
    //会员统计
    $night = strtotime(date('Y-m-d', time()));
    $day = strtotime(date('Y-m-d', time()))+86400; //86400为一天的秒数
    $where['create_time'] = ['between',$night.",".$day];
    $user = db('user')->where($where)->count();
    $users = db('user')->count();

    //收入统计
    $month = strtotime(date('Y-m-01',time()));
    $j = date("t"); //获取当前月份天数
    $months = strtotime(date('Y-m-01',time()))+(86400*$j);
    $map['create_time'] = ['between',$month.",".$months];
    $income = db('log')->where($where)->where('caozuo','消费')->sum('balance');
    $incomes = db('log')->where($map)->where('caozuo','消费')->sum('balance');

    //支出统计
    $finance = db('log')->where($where)->where('caozuo','支出')->sum('balance');
    $finances = db('log')->where($map)->where('caozuo','支出')->sum('balance');

    //收支统计
    $total = $income - $finance;
    $totals = $incomes - $finances;
    $year = date('Y',time());
    //判断平年闰年 获取当年天数
    $yearDay = date('z' , mktime(23,59,59,12,31,$year))+1;
    $yearTime = strtotime(date('Y-01-01',time()));
    $yearTimes = strtotime(date('Y-01-01',time()))+(86400*$yearDay);
    $yes['create_time'] = ['between',$yearTime.",".$yearTimes];
    $Yesincomes = db('log')->where($yes)->where('caozuo','消费')->sum('balance');
    $Yesfinances = db('log')->where($yes)->where('caozuo','支出')->sum('balance');
    $years = $Yesincomes - $Yesfinances;

    //每月收入列表
    $prefix = config('database.prefix');
    // x 轴数据，作为 x 轴标注
    $j = date("t"); //获取当前月份天数
    $start_time = strtotime(date('Y-m-01'));  //获取本月第一天时间戳
    $xData = array();
    $xx = array();
    for($i=0;$i<$j;$i++)
    {
      $xData[] = date('Y-m-d',$start_time+$i*86400); //每隔一天赋值给数组
      $xx[] = date('d',$start_time+$i*86400);
    }
    foreach($xData as $k=>$v)
    {
      $semRes = Db::query("select FROM_UNIXTIME(create_time,'%Y-%m-%d') days,sum(balance) sum from ".$prefix."log WHERE FROM_UNIXTIME(create_time,'%Y-%m-%d') = '".$v."' and caozuo = '消费'");
      $semRests[] = $semRes[0];
      $SeoData = Db::query("select FROM_UNIXTIME(create_time,'%Y-%m-%d') days,sum(balance) sum from ".$prefix."log WHERE FROM_UNIXTIME(create_time,'%Y-%m-%d') = '".$v."' and caozuo = '支出'");
      $SeoDatas[] = $SeoData[0];
    }
    //处理获取到的数据
    $ySemData = array();
    if(!empty($semRests))
    {
      foreach ($xData as $k=>$v)
      {
        foreach ($semRests as $kk=>$vv)
        {
          if($v == $vv['days'])
          {
            $ySemData[$k] = $vv['sum'];
            break;
          }else{
            $ySemData[$k] = 0;
            continue;
          }
        }
      }
    }else{
      foreach ($xData as $k=>$v)
      {
        $ySemData[$k] = 0;
      }
    }

    $ySeoData = array();
    if(!empty($SeoDatas))
    {
      foreach ($xData as $k=>$v)
      {
        foreach ($SeoDatas as $kk=>$vv)
        {
          if($v == $vv['days'])
          {
            $ySeoData[$k] = $vv['sum'];
            break;
          }else{
            $ySeoData[$k] = 0;
            continue;
          }
        }
      }
    }else{
      foreach ($xData as $k=>$v)
      {
        $ySeoData[$k] = 0;
      }
    }
    $this->assign(array(
      'user' => $user,
      'users' => $users,
      'income' => $income,
      'incomes' => $incomes,
      'ySemData' => $ySemData,
      'ySeoData' => $ySeoData,
      'xx' => $xx,
      'finance' => $finance,
      'finances' => $finances,
      'total' => $total,
      'totals' => $totals,
      'years' => $years
    ));
    return view();
  }
}
?>